package managedbean.CRM.SaleLead;

import entity.CRM.Representative;
import entity.CRM.SaleLead;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.CRM.SaleLeadBeanLocal;

@ManagedBean(name="editSaleLeadManagerBean")
@ViewScoped

public class EditSaleLeadManagerBean implements Serializable
{
    @EJB
    private SaleLeadBeanLocal saleLeadBeanLocal;
    private SaleLead saleLeadToEdit;
    private String statusMessage;
    private Representative selectedRep;
    List<Representative> reps;
    private Boolean display;
    
    Representative newRep = new Representative();
    
    public SaleLead getSaleLeadToEdit() {
        return saleLeadToEdit;
    }
    public void setSaleLeadToEdit(SaleLead saleLeadToEdit) {
        this.saleLeadToEdit = saleLeadToEdit;
    }

    public String getStatusMessage() {
        return statusMessage;
    }
    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public Representative getNewRep() {
        return newRep;
    }

    public void setNewRep(Representative newRep) {
        this.newRep = newRep;
    }

    public List<Representative> getReps() {
        return reps;
    }

    public void setReps(List<Representative> reps) {
        this.reps = reps;
    }

    public Representative getSelectedRep() {
        return selectedRep;
    }

    public void setSelectedRep(Representative selectedRep) {
        this.selectedRep = selectedRep;
    }

    public Boolean getDisplay() {
        return display;
    }

    public void setDisplay(Boolean display) {
        this.display = display;
    }
    
    
    
    public EditSaleLeadManagerBean() {}
    
    public void initView(PhaseEvent event){
        if(saleLeadToEdit == null)
        {            
            saleLeadToEdit = (SaleLead)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedSaleLead");            
            this.setReps(saleLeadToEdit.getRepresentatives());
        }
    }
    
    public void updateExistingSaleLead(ActionEvent event)
    {
        try {
            saleLeadBeanLocal.updateSaleLead(saleLeadToEdit.getRegistrationNumber(), saleLeadToEdit.getSaleLeadName(), saleLeadToEdit.getSaleLeadAddress(), saleLeadToEdit.getCustomerType(), saleLeadToEdit.getRepresentatives());
            statusMessage = "Sale Lead Updated Successfully";

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Update Sale Lead Result: " + statusMessage + ".", ""));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void add(ActionEvent event)
    {
        this.setDisplay(true);
    }
    
    
    public void addRep(ActionEvent event)
    {
        
        Representative temp = this.newRep;
        reps.add(temp);
        newRep = new Representative();
    }
    
    public void editRep(ActionEvent event)
    {
        Representative temp = this.selectedRep;
        
        for (int i = 0; i < reps.size(); i++)
        {
            if (reps.get(i).getEmail() == temp.getEmail())
            {
                reps.get(i).setName(temp.getName());
                reps.get(i).setContactNumber(temp.getContactNumber());
            }
        }
        
    }
    
    public void deleteRep(ActionEvent event)
    {
        Representative temp = this.selectedRep;
        for (int i = 0; i < reps.size(); i++)
        {
            if (reps.get(i).getEmail() == temp.getEmail())
            {
                reps.remove(i);
            }
        }
    }
    
}
